/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package meteocal.gui;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.context.SessionScoped;
import javax.persistence.EntityManager;
import meteocal.business.boundary.EventFacade;
import meteocal.business.boundary.NotificationFacade;
import meteocal.business.boundary.UserFacade;
import meteocal.business.entity.Event;
import meteocal.business.entity.Notification;
import meteocal.business.security.boundary.UserManager;
import meteocal.business.security.entity.User;
import meteocal.gui.security.UserBean;

/**
 *
 * @author Francesca
 */
@Named(value = "notificationBean")
@SessionScoped
public class NotificationBean implements Serializable{

    @EJB
    NotificationFacade nf;
    
    @EJB
    UserManager um;
    
    @EJB
    EventFacade ef;
    
    @EJB
    UserFacade uf;
   
    
    private ShowCalendarBean showCalendarBean;
    
    private List<Notification> invitationList;
    private Collection<User> invited;
    
    private Integer idNotification;
    private String idPending;
    private Integer idEventToUpdate;
    private Integer idEventSelected;
    private Notification notification;
    private Event eventSelected;
    private Event eventToUpdate;
    private User user;
    
    public NotificationBean() {
        showCalendarBean = new ShowCalendarBean();
    }
    
    public List<Notification> checkNotificationInvitation(String loggedUser){
        invitationList = new ArrayList<>();
        invitationList = nf.checkInvitation(um.getLoggedUser().getUsername());
        return invitationList;
    }
    
    /**
     * When a user doesn't want to participate to an event
     * the notification is deleted from the notification table 
     * and he's deleted from the pending table
     */
    //public String deleteInvitedUser(){
        /*idNotification = showCalendarBean.InvitationIdFromEvent();
        System.out.println("elimino l'evento "+idNotification);
        nf.removeNotification(idNotification);
        idPending = um.getLoggedUser().getUsername();
        System.out.println("elimino l'utente "+idPending+"dai pending");
        idEventToUpdate = showCalendarBean.getIdEventToUpdate();
        System.out.println(idEventToUpdate);
        ef.removeEvent(idEventToUpdate);*/
       /* Integer type = 1;
        user = um.getLoggedUser();
        System.out.println(user.getUsername());
        //System.out.println(showCalendarBean.getEventSelected().getName());
        notification = selectIdNotification(eventSelected,user,type);
        System.out.println("elimino l'evento "+idNotification);
        nf.removeNotification(notification.getIdNotification());
        System.out.println("elimino l'utente "+idPending+"dai pending");
        eventToUpdate = ef.eventDetails(eventSelected.getIdEvent());
        eventToUpdate.getPendingUsers().remove(user);
        nf.entityManager();
        ef.entityManager();
        return "main-page?faces-redirect=true";
    }
    
    /**
     * This event find the notification id 
     * where the event is the one selected 
     * the type is one
     * and where the receiver is the logged user
     * 
     */
    
   /* public Notification selectIdNotification(Event event, User user, Integer type){
        return nf.checkIdNotification(event,user,type);
    }*/
    
    /**
     * When a user accept the invitation the notification is deleted 
     * and the user is inserted into invited users
     
    public String insertInvitedUser(){ 
        idNotification = showCalendarBean.InvitationIdFromEvent();
        nf.removeNotification(idNotification);
        idPending = um.getLoggedUser().getUsername();
        idEventToUpdate = showCalendarBean.getIdEventToUpdate();
        User user = uf.find(idPending);
        invited.add(user);
        Event e = ef.eventDetails(idEventToUpdate);
        e.setParticipants(invited);
        nf.entityManager();
        return "main-page?faces-redirect=true";
    }
*/
    public Event getEventSelected() {
        return eventSelected;
    }

    public void setEventSelected(Event eventSelected) {
        this.eventSelected = eventSelected;
    }
    
    
}